<template>
	<div class="container">
		<uni-row class="header">
			<uni-col :span="3">
				<navigator url="/pages/userinfo/userinfo">
					<uni-icons type="contact" size="40"></uni-icons>
				</navigator>
			</uni-col>
			<uni-col :span="3" :offset="18">
				<navigator url="/pages/msg/msg">
					<uni-badge type="error" :text="msgCount" absolute="rightTop">
						<uni-icons type="chat" size="40"></uni-icons>
					</uni-badge>
				</navigator>
			</uni-col>
		</uni-row>
		<uni-row>
			<uni-col :span="24">
				<swiper circular class="image">
					<swiper-item class="image" v-for="(item,index) in imgList" :key="index">
						<image :src="item" mode="aspectFill" class="image">
						</image>
					</swiper-item>
				</swiper>
			</uni-col>
		</uni-row>
		<uni-card title="快捷入口">
			<uni-row justify="space-between" type="flex">
				<uni-col v-for="(item,index) in services" :key="index" :span="4" style="text-align: center" @click="goTo(item.url)">
          <image :src="item.image" class="cover" mode="aspectFill"></image>
          <span class="title">{{ item.name }}</span>
				</uni-col>
			</uni-row>
      <uni-row justify="center" type="flex" style="margin-bottom: 20px;margin-top: 20px">
        <uni-col :span="5" style="text-align: center">
          <image src="../../static/indexrefrash.png" @click="refreshAll" style="height: 80px;width: 80px"></image>
          <span class="title">刷新数据</span>
        </uni-col>
        <uni-col :span="5" style="text-align: center;">
          <image src="../../static/indexsyanc.png" @click="syncAll" style="height: 80px;width: 80px"></image>
          <span class="title">同步数据</span>
        </uni-col>
      </uni-row>
		</uni-card>
    <uni-card title="幼虫录入待同步">
      <template v-slot:title>
        <uni-list>
          <uni-list-item @switchChange="handleInputSwitch" :show-switch="true" title="幼虫录入待同步"/>
        </uni-list>
      </template>
      <div class="info-box" v-for="item in enterings" :key="item.enter.id" @click="handleUpdate(item)">
        <uni-row>
          <uni-col :span="16">
            <uni-title :title="item.enter.batchNumber" type="h1"/>
          </uni-col>
          <uni-col :span="8" class="status">
            <uni-tag v-if="item.change != null"
                     text="待同步"
                     circle
                     inverted
                     style="margin-right: 15px"></uni-tag>
          </uni-col>
        </uni-row>
        <uni-row>
          <uni-col :span="21">
            <uni-row>
              <uni-col :span="12" class="text">
                类&nbsp;&nbsp;&nbsp;&nbsp;型 : {{ item.enter.bugType }}
              </uni-col>
              <uni-col :span="12" class="text">
                养&nbsp;&nbsp;虫&nbsp;&nbsp;车&nbsp;&nbsp;间 : {{item.enter.roomName}}
              </uni-col>
              <uni-col :span="12" class="text">
                幼 虫 数 量 : {{item.enter.sum}}
              </uni-col>
              <uni-col :span="12" class="text">
                下次换料时间 : {{item.enter.nextReloadingDate}}
              </uni-col>
              <uni-col :span="12" class="text">
                操 作 人 员 : {{item.enter.userName}}
              </uni-col>
            </uni-row>
          </uni-col>
          <uni-col :span="3">
<!--            <image @click="handleDelete(item)" v-if="item.change != null" src="../../static/trash.png" style="width: 60px;height: 70px;margin-top: 5px"></image>-->
          </uni-col>
        </uni-row>
      </div>
    </uni-card>
    <uni-card>
      <template v-slot:title>
        <uni-list>
          <uni-list-item @switchChange="handleFeedSwitch" :show-switch="true" title="饲养记录待同步"/>
        </uni-list>
      </template>
      <div class="info-box" v-for="item in feeds" :key="item.feed.id" @click="handleFeedUpdate(item.feed.id)">
        <uni-row>
          <uni-col :span="16">
            <uni-title :title="item.feed.batchNumber" type="h1"/>
          </uni-col>
          <uni-col :span="8" class="status">
            <uni-tag v-if="item.change != null" :text="item.change.changeStatus == '2' ? '已拒绝' : '待记录'"
                     circle inverted style="margin-right: 15px"></uni-tag>
          </uni-col>
        </uni-row>
        <uni-row>
          <uni-col :span="21">
            <uni-row>
              <uni-col :span="12" class="text">
                类&nbsp;&nbsp;&nbsp;&nbsp;型 : {{ item.feed.bugType }}
              </uni-col>
              <uni-col :span="12" class="text">
                轮&nbsp;&nbsp;&nbsp;&nbsp; 次 : {{ item.feed.round }}
              </uni-col>
              <uni-col :span="12" class="text">
                换 虫 数 量 : {{ item.feed.changeNum }}
              </uni-col>
              <uni-col :span="12" class="text">
                周&nbsp;&nbsp;&nbsp;&nbsp;期 : {{ item.feed.period != '0' ? '26天' : '28天' }}
              </uni-col>
              <uni-col :span="12" class="text">
                本次换料时间 : {{ item.feed.reloadDay }}
              </uni-col>
              <uni-col :span="12" class="text">
                挑 &nbsp; 虫 &nbsp; 月 &nbsp; 份 : {{ item.feed.optionMonth }}
              </uni-col>
            </uni-row>
          </uni-col>
          <uni-col :span="3">
          </uni-col>
        </uni-row>
      </div>
    </uni-card>
    <uni-card >
      <template v-slot:title>
        <uni-list>
          <uni-list-item @switchChange="handleFeed2Switch" :show-switch="true" title="饲养记录待记录"/>
        </uni-list>
      </template>
      <div class="info-box" v-for="item in feedss" :key="item.feed.id" @click="handleFeedUpdate(item.feed.id)">
        <uni-row>
          <uni-col :span="16">
            <uni-title :title="item.feed.batchNumber" type="h1"/>
          </uni-col>
          <uni-col :span="8" class="status">
            <uni-tag v-if="item.change != null" :text="item.change.changeStatus == '2' ? '已拒绝' : '待记录'"
                     circle inverted style="margin-right: 15px"></uni-tag>
          </uni-col>
        </uni-row>
        <uni-row>
          <uni-col :span="21">
            <uni-row>
              <uni-col :span="12" class="text">
                类&nbsp;&nbsp;&nbsp;&nbsp;型 : {{ item.feed.bugType }}
              </uni-col>
              <uni-col :span="12" class="text">
                轮&nbsp;&nbsp;&nbsp;&nbsp; 次 : {{ item.feed.round }}
              </uni-col>
              <uni-col :span="12" class="text">
                换 虫 数 量 : {{ item.feed.changeNum }}
              </uni-col>
              <uni-col :span="12" class="text">
                周&nbsp;&nbsp;&nbsp;&nbsp;期 : {{ item.feed.period != '0' ? '26天' : '28天' }}
              </uni-col>
              <uni-col :span="12" class="text">
                本次换料时间 : {{ item.feed.reloadDay }}
              </uni-col>
              <uni-col :span="12" class="text">
                挑 &nbsp; 虫 &nbsp; 月 &nbsp; 份 : {{ item.feed.optionMonth }}
              </uni-col>
            </uni-row>
          </uni-col>
          <uni-col :span="3">
          </uni-col>
        </uni-row>
      </div>
    </uni-card>
    <uni-card title="饲养记录待审批">
      <template v-slot:title>
        <uni-list>
          <uni-list-item @switchChange="handleAuditSwitch" :show-switch="true" title="饲养记录待审批"/>
        </uni-list>
      </template>
      <div class="info-box" v-for="item in audits" @click="handleAuditUpdate(item.feed.id)">
        <uni-row>
          <uni-col :span="16">
            <uni-title :title="item.feed.batchNumber" type="h1"/>
          </uni-col>
          <uni-col :span="8" class="status">
            <uni-tag v-if="item.change != null"
                     text="待同步"
                     circle
                     inverted
                     style="margin-right: 15px"
            ></uni-tag>
            <uni-tag :text="item.feed.tableStatus === '2' ? '已拒绝' : item.feed.tableStatus == '0' ? '通过' : '待审批'"
                     circle
                     inverted
                     :type="item.feed.tableStatus === '2' ? 'error' : item.feed.tableStatus == '0' ? 'success' : 'primary'"
                     style="margin-right: 15px"></uni-tag>
          </uni-col>
        </uni-row>
        <uni-row>
          <uni-col :span="21">
            <uni-row>
              <uni-col :span="12" class="text">
                类&nbsp;&nbsp;&nbsp;&nbsp;型 : {{ item.feed.bugType }}
              </uni-col>
              <uni-col :span="12" class="text">
                轮&nbsp;&nbsp;&nbsp;&nbsp; 次 : {{ item.feed.round }}
              </uni-col>
              <uni-col :span="12" class="text">
                换 虫 数 量 : {{ item.feed.changeNum }}
              </uni-col>
              <uni-col :span="12" class="text">
                周&nbsp;&nbsp;&nbsp;&nbsp;期 : {{ item.feed.period != '0' ? '26天' : '28天' }}
              </uni-col>
              <uni-col :span="12" class="text">
                本次换料时间 : {{ item.feed.reloadDay }}
              </uni-col>
              <uni-col :span="12" class="text">
                挑 &nbsp; 虫 &nbsp; 月 &nbsp; 份 : {{ item.feed.optionMonth }}
              </uni-col>
            </uni-row>
          </uni-col>
          <uni-col :span="3">

          </uni-col>
        </uni-row>
      </div>
    </uni-card>
<!--		<uni-row>-->
<!--			<uni-col>-->
<!--				<button @click="refreshDeptList">刷新部门</button>-->
<!--			</uni-col>-->
<!--			<uni-col>-->
<!--				<button @click="refrashRoomList">刷新房间</button>-->
<!--			</uni-col>-->
<!--		</uni-row>-->
	</div>
</template>

<script setup lang="ts">
	import {
		mapGetters,
		useStore
	} from "vuex";
	import UniRow from "../../uni_modules/uni-row/components/uni-row/uni-row.vue";
	import UniCol from "../../uni_modules/uni-row/components/uni-col/uni-col.vue";
	import UniIcons from "../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
	import UniEasyinput from "../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue";
	import UniBadge from "../../uni_modules/uni-badge/components/uni-badge/uni-badge.vue";
  import {
    computed,
    ref, watch
  } from "vue";
	import UniSwiperDot from "../../uni_modules/uni-swiper-dot/components/uni-swiper-dot/uni-swiper-dot.vue";
	import UniSwipeAction from "../../uni_modules/uni-swipe-action/components/uni-swipe-action/uni-swipe-action.vue";
	import UniCard from "../../uni_modules/uni-card/components/uni-card/uni-card.vue";
	import {
		onLoad
	} from "@dcloudio/uni-app";
	import {
		MsgMapper
	} from "../../mappers/MsgMapper";
	import {
		loginService
	} from "../../pagesVM/login";
	import {
		msgService
	} from "../../pagesVM/msg";
	import {
		deptList,
		RoomList
	} from "../../api/user";
  import {
    BugEnterVo,
    BugFeedVo,
    Dept,
    Room
  } from "../../sql/domin";
	import {
		RoomMapper
	} from "../../mappers/RoomMapper";
	import {
		DeptMapper
	} from "../../mappers/DeptMapper";
  import {inputService} from "../../pagesVM/input";
  import {FeedClickHandle, FeedDataHandle} from "../../pagesVM/feed";
  import {AuditClickHandle, AuditDataHandle} from "../../pagesVM/audit";
  import UniListItem from "../../uni_modules/uni-list/components/uni-list-item/uni-list-item.vue";
  import {PlanDataHandle} from "../../pagesVM/plan";
  import {ReportDataHandle} from "../../pagesVM/report";

	const msgMapper = MsgMapper

	const {
		onlineLogin,
		getNetWorkType,
		checkToken
	} = loginService()

	const {
		refreshMsg,
		getMsgCount
	} = msgService()

  const audits = ref<Array<BugFeedVo>>()
  const feeds = ref<Array<BugFeedVo>>()
  const enterings = ref<BugEnterVo[]>()
  const feedss = ref<Array<BugFeedVo>>()

  const showAudit = ref(false)
  const showFeed = ref(false)
  const showFeed2 = ref(false)
  const showInput = ref(false)

  const handleAuditSwitch = (val:any) => {
    showAudit.value = val.value
  }
  const handleFeedSwitch = (val:any) => {
    showFeed.value = val.value
  }
  const handleFeed2Switch = (val:any) => {
    showFeed2.value = val.value
  }
  const handleInputSwitch = (val:any) => {
    showInput.value = val.value
  }

  watch(showAudit,async (value)=>{
    if (value){
      audits.value = (await getAuditList()).filter(it=>it.change == null && it.feed.tableStatus == '1')
    }else {
      audits.value = []
    }
  })

  watch(showFeed,async (value)=>{
    if (value){
      feeds.value = (await getFeedList()).filter(it=>it.change != null)
    }else {
      feeds.value = []
    }
  })

  watch(showFeed2,async (value)=>{
    if (value){
      feedss.value = (await getFeedList()).filter(it=>it.feed.tableStatus == '0')
    }else {
      feedss.value = []
    }
  })

  watch(showInput,async (value)=>{
    if (value){
      enterings.value =  (await getList()).filter(it=>it.change != null)
    }else {
      enterings.value = []
    }
  })

  const {handleUpdate,handleDelete,getList,refrashEnter,syncEnter} = inputService()
  const {getList:getFeedList,syncFeed,refrashFeedList,refrashFeedsFuture} = FeedDataHandle()
  const {handleUpdate:handleFeedUpdate} = FeedClickHandle()
  const {
    getList:getAuditList,
    handleUpdate:handleAuditUpdate
  } = AuditClickHandle()
	const store = useStore()

  const goTo = (url:string) => {
    uni.navigateTo({
      url:url
    })
  }

  const {refreshAudits,syncAudits} = AuditDataHandle()

  const {refreshPlan} = PlanDataHandle()

  const {refrashReport} = ReportDataHandle()

	const msgCount = computed(mapGetters(['msgCount']).msgCount.bind({
		$store: store
	}))

  const refreshAll = async () => {
    if (await getNetWorkType()) {
      await getMsgCount()
      await refrashRoomList()
      await refreshDeptList()
      await refrashReport()
      await refreshPlan()
      await refrashFeedList()
      await refreshAudits()
      await refrashEnter()
	  await refrashFeedsFuture()
    }
  }

  const syncAll = async () => {
    if (await getNetWorkType()) {
      let count = []
      count.push(await syncAudits())
      count.push(await syncEnter())
      count.push(await syncFeed())
      uni.showToast({
        icon:"none",
        title:`${count[0] == 0 ? '' : `已同步${count[0]}条审批 `}${count[1] == 0 ? '' : `已同步${count[1]}条幼虫录入 `}${count[2] == 0 ? '' : `已同步${count[2]}条饲养记录 `}`
      })
    }
  }

	onLoad(async () => {
		await checkToken()
		uni.hideLoading()
		if (await getNetWorkType()) {
			let user = store.getters.userinfo
			await refreshMsg(user.id)
			// await refrashRoomList()
			// await refreshDeptList()
		}

		if (store.getters.role == 'czy') {
			services.value.slice(1, 2)
		}
	})

	const option = {
		centeredSlides: true
	}
	// 刷新房间
	const refrashRoomList = () => {
		RoomList().then((res) => {
			let list = res.data.data as Room[]
			list.forEach(it => {
				it.params = null
			})
			if (list.length != 0) {
				RoomMapper.deleteRoomListAll().then(() => {
					RoomMapper.insertRoomList(list).then(() => {
						uni.showToast({
							title: `已刷新${list.length}个房间到本地`,
							duration: 2000
						})
					})
				})
			}
		})
	}

	const refreshDeptList = () => {
		deptList().then((res) => {
			let list = res.data.data as Dept[]
			list.forEach(it => {
				it.params = null
				it.children = null
			})
			if (list.length != 0) {
				DeptMapper.deleteDept().then(() => {
					DeptMapper.insertDept(list).then(() => {
						uni.showToast({
							title: `已刷新${list.length}个部门到本地`,
							duration: 2000
						})
					})
				})
			}
		})
	}

	type service = {
		name: string
		url: string
		image: any
	}

	const services = ref < service[] > ([{
			name: '新幼虫录入',
			url: '/pages/input/input',
			image: '../../static/input.png'
		},
		{
			name: '正常饲养记录',
			url: '/pages/feed/feed',
			image: '../../static/feed.png'
		},
		{
			name: '审批饲养记录',
			url: '/pages/audit/audit',
			image: '../../static/audit.png'
		},
		{
			name: '车间数据报表',
			url: '/pages/report/report',
			image: '../../static/report.png'
		},
		{
			name: '车间生产计划',
			url: '/pages/plan/plan',
			image: '../../static/plan.png'
		}
	])




	const imgList = [
		'../../assets/image/login.png',
		'../../assets/image/logo.png',
		'../../assets/image/login.png',
		'../../assets/image/login.png',
		'../../assets/image/logo.png'
	]
</script>

<style scoped>
	* {
		margin: 0;
		padding: 0;
	}

	.container {
		background-color: #f0f0f0;
	}

	.header {
		padding-top: 15px;
		padding-bottom: 15px;
		background-color: rgb(240, 240, 240);
		text-align: center;
	}

	.image {
		height: 300px;
		width: 100%;
    background-size: cover;
    background-position: right top;
    background-repeat: no-repeat;
	}


	.service-box {
		height: 120px;
		width: 90px;
		background-color: #ffffff;
		border-radius: 10px;
		text-align: center;
	}

	.title {
		display: block;
		position: center;
		bottom: 10px;
		left: 30px;
		font-weight: bold;
		font-size: small;
	}

	.cover {
		height: 80px;
		width: 80px;
		margin-top: 10px;
	}
</style>
